package wiki.hitime.stl.daq.service.admin;

import wiki.hitime.stl.daq.bean.base.NameValue;
import wiki.hitime.stl.daq.bean.bo.DataCheckConfigBO;
import wiki.hitime.stl.daq.bean.dto.config.DataCheckConfigDTO;
import wiki.hitime.stl.daq.bean.dto.manage.DbTypeConfigDTO;
import wiki.hitime.stl.daq.bean.dto.manage.MsgRuleDetailConfigDTO;
import wiki.hitime.stl.daq.bean.dto.req.*;
import wiki.hitime.stl.daq.enums.DataAcquisitionTypeEnum;
import wiki.hitime.stl.daq.enums.DataValueTypeEnum;
import wiki.hitime.stl.daq.enums.MsgTypeEnum;
import wiki.hitime.stl.daq.manager.*;
import wiki.hitime.stl.daq.service.factory.DataCheckFactory;
import wiki.hitime.stl.daq.service.factory.DataMsgFactory;
import wiki.hitime.stl.daq.support.ContextHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 配置参数管理
 *
 * @author zhangdaoping
 * @create 2019-07-24 10:02
 */
@Service
public class ConfigManageService {

    private DaqDbConfigManager daqDbConfigManager;
    private DaqMsgRuleDetailConfigManager daqMsgRuleDetailConfigManager;
    private DataMsgParamManager dataMsgParamManager;
    private DaqDbDetailConfigManager daqDbDetailConfigManager;
    private DaqMsgBaseConfigManager daqMsgBaseConfigManager;

    public ConfigManageService(DaqDbConfigManager daqDbConfigManager,
                               DaqMsgRuleDetailConfigManager daqMsgRuleDetailConfigManager,
                               DataMsgParamManager dataMsgParamManager,
                               DaqDbDetailConfigManager daqDbDetailConfigManager,
                               DaqMsgBaseConfigManager daqMsgBaseConfigManager) {
        this.daqDbConfigManager = daqDbConfigManager;
        this.daqMsgRuleDetailConfigManager = daqMsgRuleDetailConfigManager;
        this.dataMsgParamManager = dataMsgParamManager;
        this.daqDbDetailConfigManager = daqDbDetailConfigManager;
        this.daqMsgBaseConfigManager = daqMsgBaseConfigManager;
    }

    /**
     * 获取所有可用的数据类型配置
     *
     * @return
     */
    public List<DbTypeConfigDTO> listDbTypeConfig() {
        return daqDbConfigManager.listDbTypeConfig();
    }

    /**
     * 获取消息类型集合
     *
     * @return
     */
    public List<NameValue> listMsgType() {
        Map<String, DataMsgFactory> objectMap = ContextHolder.getBeanOfType(DataMsgFactory.class);
        List<NameValue> msgTypeList = new ArrayList<>(objectMap.size());
        objectMap.forEach((s, dataMsgFactory) -> {
            MsgTypeEnum msgTypeEnum = dataMsgFactory.getSupportMsgType();
            NameValue nameValue = new NameValue(msgTypeEnum.getMsg(), msgTypeEnum.getStatus());
            msgTypeList.add(nameValue);
        });
        return msgTypeList;
    }


    /**
     * 获取采集类型集合
     *
     * @return
     */
    public List<NameValue> listAcquisitionType() {
        return DataAcquisitionTypeEnum.getList();
    }

    /**
     * 获取数据采集类型一致的数据检查实现类名
     *
     * @param reqDTO
     * @return
     */
    public List<DataCheckConfigDTO> listCheckServiceConfig(CheckServiceConfigListReqDTO reqDTO) {
        List<DataCheckConfigDTO> checkConfigList = new ArrayList<>();
        Map<String, DataCheckFactory> objectMap = ContextHolder.getBeanOfType(DataCheckFactory.class);
        for (Map.Entry entry : objectMap.entrySet()) {
            DataCheckFactory factory = (DataCheckFactory) entry.getValue();
            DataCheckConfigBO checkConfigBO = factory.getCheckConfig();
            DataCheckConfigDTO checkConfigDTO = new DataCheckConfigDTO();
            if (checkConfigBO.getSupportAcquisitionType().equals(reqDTO.getAcquisitionType())
                    && checkConfigBO.getSupportDataValueTypeList().contains(reqDTO.getDataValueType())) {
                BeanUtils.copyProperties(checkConfigBO, checkConfigDTO);
                checkConfigList.add(checkConfigDTO);
            }
        }
        return checkConfigList;
    }

    /**
     * 获取消息详细配置集合
     *
     * @param reqDTO
     * @return
     */
    public List<MsgRuleDetailConfigDTO> listMsgDetailConfig(MsgRuleDetailConfigListReqDTO reqDTO) {
        return daqMsgRuleDetailConfigManager.listDTO(reqDTO);
    }

    public List<NameValue> listDataValueType() {
        return DataValueTypeEnum.getList();
    }

    /**
     * 获取消息参数键集合
     *
     * @return
     */
    public List<String> listMsgParamKey() {
        return dataMsgParamManager.listMsgParamKey();
    }

    /**
     * 添加数据库类型配置
     *
     * @param reqDTO
     */
    public void addDbTypeConfig(DbTypeConfigAddReqDTO reqDTO) {
        daqDbConfigManager.addDbTypeConfig(reqDTO.getDbTypeConfigDTO());
    }

    /**
     * 添加数据库详细配置
     *
     * @param reqDTO
     */
    public void addDbDetailConfig(DbDetailConfigAddReqDTO reqDTO) {
        daqDbDetailConfigManager.addDbDetailConfig(reqDTO.getDbDetailConfigDTOList());
    }

    /**
     * 添加消息基础配置
     *
     * @param reqDTO
     */
    public void addMsgBaseConfig(MsgBaseConfigAddReqDTO reqDTO) {
        daqMsgBaseConfigManager.addMsgBaseConfig(reqDTO.getMsgBaseConfigDTO());
    }
}
